{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% import "_my_wtf_macros.html" as my_wtf_macros %}
{% import "_macros.html" as macros %}

{% block title %}万屏时代OPC--微信地推用户列表{% endblock %}

{% block panel_heading %}
    万屏时代OPC--微信地推用户列表
{% endblock %}

{% block panel_body %}

    <form id="search-demand-form"
          action="{{ url_for('opc_wechat_bp.ground_user_list') }}" method="post"
          class="form" role="form">
        {{ my_wtf_macros.form_field(form.nickname, div_col=4, label_col=4, field_col=7) }}
        {{ my_wtf_macros.form_field(form.subscribe, div_col=4, label_col=4, field_col=7) }}
        {{ my_wtf_macros.form_field(form.invite_user_id, div_col=4, label_col=4, field_col=7) }}
        {{ my_wtf_macros.form_field(form.subscribe_time_begin, div_col=4, label_col=4, field_col=7) }}
        {{ my_wtf_macros.form_field(form.subscribe_time_end, div_col=4, label_col=4, field_col=7) }}
        {{ my_wtf_macros.form_field(form.is_invite_user, div_col=4, label_col=4, field_col=7) }}
        <div class="pull-right btn-group">
            <button type="submit" class="btn btn-info"
                    id="query-demand-btn">
                查询
            </button>
            <a
                    href="javascript:void(0);"
                    name="user-info"
                    target="_blank"
                    class="btn-group btn btn-primary"
                    id="export-user-info"
            >
                导出
            </a>
        </div>
    </form>

    {% if pagination %}
        <div class="pagination">
            {{ macros.pagination_widget(pagination, 'opc_wechat_bp.ground_user_list', **url_kwargs) }}
        </div>
    {% endif %}

    <table id="wechat_template_table"
           class="table table-bordered table-hover table-condensed table-striped">
        <thead>
        <tr>
            <th>昵称</th>
            <th>电话号码</th>
            <th>关注时间</th>
            <th>关注状态</th>
            <th>邀请人</th>
            <th>邀请时间</th>
            <th>IP</th>
            <th>IP城市</th>
            <th>GPS定位</th>
            <th>绑定时间</th>
            <th>审核状态</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        {% for user in wechat_users %}
            <tr {% if user.is_in_range %}class="success"
                {% else %}class="danger"{% endif %}>
                <td>
                    {% if user.user %}
                        <a href="{{ url_for('op_bp.user_info', user_id=user.user.id) }}">{{ user.nickname }}</a>
                    {% else %}
                        {{ user.nickname }}
                    {% endif %}
                </td>
                <td>{{ user.user.mobile | encrypt }}</td>
                <td>{{ user.subscribe_time_display }}</td>
                <td>{{ user.subscribe_display }}</td>
                <td>
                    {% if user.invite_user %}
                        <a href="{{ url_for('op_bp.user_info', user_id=user.invite_user.id) }}">{{ user.invite_user.nickname }}
                        </a>
                    {% else %}
                        无
                    {% endif %}
                </td>
                <td>{{ user.invite_time_display or '' }}</td>
                <td>{{ user.ip_address }}</td>
                <td>{{ user.ip_city }}</td>
                <td>{{ user.coordinates_display }}</td>
                <td>{{ user.certify_time_display }}</td>
                <td>{{ user.approval_status_display }}</td>
                <td>
                    {% if user.approval_status == 0 %}
                        <a href="javascript:void(0);"
                           class="btn btn-success btn-xs user-approval-btn"
                           data-user_id="{{ user.id }}"
                           data-status="1"
                        >通过</a>
                        <a href="javascript:void(0);"
                           class="btn btn-danger btn-xs user-approval-btn"
                           data-user_id="{{ user.id }}"
                           data-status="2"
                        >拒绝</a>
                    {% endif %}

                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>

    {% if pagination %}
        <div class="pagination">
            {{ macros.pagination_widget(pagination, 'opc_wechat_bp.ground_user_list', **url_kwargs) }}
        </div>
    {% endif %}
{% endblock %}

{% block scripts %}
    {{ super() }}
    <script>
        // 地推用户审核操作
        $('#wechat_template_table .user-approval-btn').each(function (i, obj) {
            $(obj).click(function () {
                var status_display_obj = $(this).parent().prev();
                var user_id = $(this).data('user_id');
                var status = $(this).data('status');
                $.ajax({
                    url: "{{ url_for('opc_wechat_bp.approval_ground_user')}}",
                    data: JSON.stringify({
                        'user_id': user_id,
                        'status': status
                    }),
                    type: 'post',
                    dataType: 'json',
                    contentType: 'application/json',
                    cache: true
                }).done(function (data) {
                    status_display_obj.html(data.approval_status_display)

                }).fail(function () {
                    $(obj).data('content', '服务器错误');
                    $(obj).popover('show');
                });
            });
        });

        $(document).ready(function () {
            $("#export-user-info").click(function () {
                var url = '{{ url_for('opc_wechat_bp.export_ground_users') }}';
                url = url + '?' + 'nickname=' + encodeURI($('#nickname').val()) +
                    '&subscribe=' + $('#subscribe').val() +
                    '&invite_user_id=' + $('#invite_user_id').val() +
                    '&subscribe_time_end=' + $('#subscribe_time_end').val() +
                    '&invite_user_id=' + $('#invite_user_id').val() +
                    '&is_invite_user=' + $('#is_invite_user').val();
                window.location.href = url;
            });
        })
    </script>
{% endblock %}

